//
//  TBCustomTabBar.m
//  Kingka
//
//  Created by Sheldon Peng on 2023/12/8.
//

#import "BJCustomTabBar.h"
#import "BJCustomButton.h"
#define kScreenW    ([UIScreen mainScreen].bounds.size.width)
#define kScreenH    ([UIScreen mainScreen].bounds.size.height)
@interface BJCustomTabBar ()

@property (nonatomic , weak) UIButton *seletedButton;

@end

@implementation BJCustomTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}


- (void)setItems:(NSArray *)items {
    
    _items = items;
    
    
    CGFloat buttonX = 20;
    CGFloat buttonY = 0;
    CGFloat buttonW = 50;
    CGFloat buttonH = 50;
    
 
    UIButton *previousButton;
    for (int i = 0; i < self.items.count; i++) {
        
        //添加子控件
        BJCustomButton *button = [[BJCustomButton alloc] init];

//            button.frame = CGRectMake(buttonX+(buttonW+20)*i, buttonY, buttonW, buttonH);
  
        [self addSubview:button];
        
        
        if(i == 0){
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(buttonY);
                make.left.mas_equalTo((kScreenW - 200)/5);
                make.height.mas_equalTo(buttonH);
                make.width.mas_equalTo(50);
                
            }];
        }else{
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(buttonY);
                make.left.equalTo(previousButton.mas_right).offset((kScreenW - 200)/5);
                make.height.mas_equalTo(buttonH);
                make.width.mas_equalTo(buttonW);
                
            }];
        }
         
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 16;
        
        UITabBarItem *tabBarItem = self.items[i];
        
        button.tag = i;
        
        [button setImage:tabBarItem.image forState:UIControlStateNormal];
        [button setImage:tabBarItem.selectedImage forState:UIControlStateSelected];
        [button setTitle:tabBarItem.title forState:UIControlStateSelected];
//        [button setTitle:@"" forState:UIControlStateNormal];
        
//        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//        button.titleLabel.font = kFont12;
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
        
        previousButton = button;
        if (i == 0) {

            self.seletedButton = button;
            [self buttonClick:button];
        }
    }
}

- (void) buttonClick:(UIButton *) button {
    
    //1、取消上一个点击的状态
    self.seletedButton.selected = NO;
//    [self.seletedButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
//    self.seletedButton.titleLabel.font = kFont(0);
//    [self.seletedButton setBackgroundColor: UIColor.clearColor];
//    self.seletedButton.frame = CGRectMake(self.seletedButton.frame.origin.x, self.seletedButton.frame.origin.y, (SCREEN_WIDTH - 100 - 110)/3, self.seletedButton.frame.size.height);
//    CGFloat buttonW = 50;
//
//    [self.seletedButton mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(buttonW);
//        
//    }];

    
    //2、选中当前点击按钮
    button.selected = YES;
//    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
   

    //3、记录当前选中按钮
    self.seletedButton = button;
    
//    self.seletedButton.titleLabel.font = kFont(12);
//    [self.seletedButton setBackgroundColor: [UIColor colorWithHexString:@"#2AEED1"]];
////    self.seletedButton.frame = CGRectMake(self.seletedButton.frame.origin.x, self.seletedButton.frame.origin.y, 110,  self.seletedButton.frame.size.height);
//    [self.seletedButton mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(50);
//    }];

    
    //4、通知外界切换子控制器
    if ([self.delegate respondsToSelector:@selector(tabBar:index:)]) {
        
        [self.delegate tabBar:self index:button.tag];
    }
}

- (void)layoutSubviews {
    
    [super layoutSubviews];

   
}
@end
